package com.etime.shuhuiyuan.services.saler.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.SalerDao;
import com.etime.shuhuiyuan.dao.SalerSelectDao;
import com.etime.shuhuiyuan.dao.UserDao;
import com.etime.shuhuiyuan.dto.SalerDto;
import com.etime.shuhuiyuan.pojo.Saler;
import com.etime.shuhuiyuan.pojo.User;
import com.etime.shuhuiyuan.services.saler.interfaces.SalerService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SalerServiceImpl2 implements SalerService2 {
    //该注解用于自动注入实例对象
    @Autowired
    private SalerDao salerDao;
    @Autowired
    private SalerSelectDao salerSelectDao;
    @Autowired
    private UserDao userDao;

    @Transactional
    @Override
    public int insertSaler(Saler saler) {


        return salerDao.insert(saler);
    }


    @Override
    public int updateSaler(Saler saler) {

        return salerDao.updateById(saler);
    }

    @Override
    public List<SalerDto> selectSalerDto(SalerDto salerDto) {
//        QueryWrapper<Saler> wrapper = new QueryWrapper<>();
//        //非空判断
//        if (saler!=null && saler.getSalerid()!=null && saler.getSalerid()!=-1){
//            wrapper.eq("salerid",saler.getSalerid());
//        }
//        if (saler!=null && saler.getSalerdeptid()!=null && saler.getSalerdeptid()!=-1){
//            wrapper.eq("salerdeptid",saler.getSalerdeptid());
//        }
        return salerSelectDao.selectAllSaler(salerDto);
    }


    @Override
    public Page<Saler> selectByPage(Saler saler, long pagenum, long pagesize) {
        Page<Saler> page = new Page<>(pagenum,pagesize);
        QueryWrapper<Saler> wrapper = new QueryWrapper<>();
        //非空判断
        if (saler!=null && saler.getSalerid()!=null && saler.getSalerid()!=-1){
            wrapper.eq("salerid",saler.getSalerid());
        }
        if (saler!=null && saler.getSalerdeptid()!=null && saler.getSalerdeptid()!=-1){
            wrapper.eq("salerdeptid",saler.getSalerdeptid());
        }

        return salerDao.selectPage(page,wrapper);
    }

    @Override
    public Page<SalerDto> selectSalerDtoByPage(SalerDto salerDto, long pagenum, long pagesize) {
        Page<SalerDto> page = new Page<>(pagenum,pagesize);
        QueryWrapper<SalerDto> wrapper = new QueryWrapper<>();
        //非空判断
        if (salerDto!=null && salerDto.getSalerid()!=null && salerDto.getSalerid()!=-1){
            wrapper.eq("salerid",salerDto.getSalerid());
        }
        if (salerDto!=null && salerDto.getSalerdeptid()!=null && salerDto.getSalerdeptid()!=-1){
            wrapper.eq("salerdeptid",salerDto.getSalerdeptid());
        }
        return salerSelectDao.selectAllSalerDto(page,wrapper);
    }

    @Override
    public Saler selectBySalerid(Integer salerid) {

        return salerDao.selectById(salerid);
    }

    @Override
    public SalerDto getSalerDtoById(Integer salerid) {
        return salerSelectDao.getSalerDtoById(salerid);
    }


    @Override
    public int updateSalerDto(SalerDto salerDto) {
        salerSelectDao.updateById(salerDto);
        return 0;
    }

    @Override
    public Page<SalerDto> selectSalerDtoByCon(SalerDto salerDto, long pagenum, long pagesize) {
        Page<SalerDto> page = new Page<>(pagenum,pagesize);
        System.out.println(salerDto);
        //QueryWrapper<SalerDto> wrapper = new QueryWrapper<>();
        String usertruename = "";
        Integer salerauditstate = null;
        //System.out.println(usertruename);
        //非空判断
        if (salerDto!=null){
            if (salerDto.getUsertruename()!=null && !salerDto.getUsertruename().equalsIgnoreCase("")){
                usertruename = salerDto.getUsertruename();
                System.out.println(usertruename);
                usertruename = "%"+usertruename+"%";
            }
            if (salerDto.getSalerauditstate()!=null && salerDto.getSalerauditstate()!=-1){
                salerauditstate = salerDto.getSalerauditstate();
            }
        }
        return salerSelectDao.selectSalerDtoByCon(page,salerDto,usertruename,salerauditstate);
    }

    @Transactional
    @Override
    public int inserUserSale(User user, Saler saler) {
        //从用户对象获取userid
        userDao.insert(user);
        user.setIssaler(1);
        int userid = user.getUserid();
        saler.setUserid(userid);
        return salerDao.insert(saler);
    }
}
